package com.service;

import com.dto.DeptDto;
import com.dto.UserDto;
import com.rowmapper.UserRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
public class UserService {

    private final JdbcTemplate jdbcTemplate;

    private final DeptService deptService;

    public UserService(JdbcTemplate jdbcTemplate,DeptService deptService) {
        this.jdbcTemplate = jdbcTemplate;
        this.deptService=deptService;
    }

    public void addUser(UserDto userDto){

        jdbcTemplate.execute("insert into user (`name`,`dept_id`) values('"+userDto.getName()+"',"+userDto.getDeptId()+")");
    }

    public List<UserDto> page(){

        return jdbcTemplate.query("select * from user",new UserRowMapper());
    }

    public void addUserWithDept(UserDto user, DeptDto deptDto) {
        addUser(user);
        deptService.addDept(deptDto);
    }
}
